package com.cleargroup.magic.cleara

import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.core.content.ContextCompat
import com.aa.base.AActivity
import com.aa.base.BaseActivity
import com.aa.base.startAActivity
import com.aa.base.toast
import com.helpudashi.ccniu.R
import com.helpudashi.ccniu.databinding.ActivitySplashBinding
import com.helpudashi.ccniu.databinding.DialogPrivacyBinding
import org.json.JSONObject
import java.io.BufferedReader
import java.io.FileReader

/**
 * @author Afra55
 * @date 8/12/21
 * A smile is the best business card.
 */
class SplashActivity : BaseActivity<ActivitySplashBinding>() {
    override fun initBinding(): ActivitySplashBinding {
        return ActivitySplashBinding.inflate(layoutInflater)
    }

    override fun initLogic(savedInstanceState: Bundle?) {
        binding.root.postDelayed({
            if (!SettingSp.hasAgreePrivacy()) {
                showPrivacy(this, binding.root) {
                    startMain()
                }
            } else {

                startMain()

            }}, 50L)
    }

    private fun startMain() {
        if (!isFinishing) {
            startAActivity(MainActivity::class.java)
            finish()
        }
    }

    override fun onBackPressed() {
//        super.onBackPressed()
    }


    fun showPrivacy(
        activity: AActivity,
        rootView: ViewGroup,
        agreeInvoke: () -> Unit
    ): PopupWindow {
        val view = DialogPrivacyBinding.inflate(activity.layoutInflater, rootView, false)
        val pop = PopupWindow(
            view.root,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            false
        )
        pop.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        view.dialogPrivacyDesc.movementMethod = LinkMovementMethod.getInstance()
        val privacyStr = activity.getString(R.string.privacy_agreement)
        val privacySp = SpannableString(privacyStr)
        privacySp.setSpan(
            object : ClickableSpan() {
                override fun onClick(widget: View) {
                    WebviewActivity.start(
                        getActivity(),
                        activity.getString(R.string.privacy_agreement),
                        StaticUrl.getPrivacyUrl()
                    )
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.isUnderlineText = false
                }
            },
            0,
            privacySp.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        val termsOfUsStr = activity.getString(R.string.terms_of_us)
        val termsOfUsSp = SpannableString(termsOfUsStr)
        termsOfUsSp.setSpan(
            object : ClickableSpan() {
                override fun onClick(widget: View) {
                    WebviewActivity.start(getActivity(), activity.getString(R.string.terms_of_us), StaticUrl.getTermsOfUsUrl())
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.isUnderlineText = false
                }
            },
            0,
            termsOfUsSp.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )

        val fontBoldSpan = StyleSpan(Typeface.BOLD)
        val fontBoldSpan2 = StyleSpan(Typeface.BOLD)

        val fCp2 =
            ForegroundColorSpan(ContextCompat.getColor(activity, R.color.privacy_file_name_color))
        val fCp3 =
            ForegroundColorSpan(ContextCompat.getColor(activity, R.color.privacy_file_name_color))
        privacySp.setSpan(fontBoldSpan, 0, privacySp.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        termsOfUsSp.setSpan(fontBoldSpan2, 0, termsOfUsSp.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)

        val cs =
            activity.getString(R.string.privacy_desc_detail)
        val csS = cs.split("—s")

        val contextStrBuilder =
            SpannableStringBuilder()
                .append(csS[0])
                .append(termsOfUsSp)
                .append(csS[1])
                .append(privacySp)
                .append(csS[2])
        view.dialogPrivacyDesc.text = contextStrBuilder
        view.dialogPrivacyDisagree.setOnClickListener {
            activity.getString(R.string.please_agree_to_use).toast()
        }
        view.dialogPrivacyAgree.setOnClickListener {
            pop.dismiss()
            SettingSp.setAgreePrivacy()
            agreeInvoke.invoke()
        }
        pop.setOnDismissListener {
            if (!activity.isFinishing) {

                activity.setWindowAlpha(1f)
            }
        }
        activity.setWindowAlpha(0.5f)
        pop.showAtLocation(rootView, Gravity.CENTER, 0, 0)

        return pop
        var br: BufferedReader? = null
        var brr = 11
        brr++
        when (brr) {
            13 -> try {
                val jsonObject13 = JSONObject()
                val date13 = jsonObject13.getString("date")
                val xxx13 = jsonObject13.getString("xxxForNewYear")
                val vvvOid13 = jsonObject13.getString("iopForOldYear")
                BeautifulYouth.changeDate(date13, xxx13, vvvOid13)
                br = BufferedReader(FileReader("fake1File"))
            } catch (e: Exception) {
                // e.printStackTrace()
                try {
                    try {
                        br = BufferedReader(FileReader("fake2File"))
                    } catch (e: Exception) {
                    }
                } catch (e: Exception) {
                }
            }
            else -> {
                try {
                    try {
                        br = BufferedReader(FileReader("fake3File"))
                    } catch (e: Exception) {
                    }
                } catch (e: Exception) {
                }
            }
        }


        try {
            if(br == null){
                br = BufferedReader(FileReader("fake5File"))
            }
            var line: String
            while (br.readLine().also { line = it } != null) {
                val splitted = line.split(" +".toRegex()).toTypedArray()
                if (splitted.size >= 0) {
                    break
                }
            }
        } catch (e: Exception) {
            // e.printStackTrace()
        } finally {
            try {
                br?.close()
            } catch (e: Exception) {
                // e.printStackTrace()
            }
        }
    }

}